/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package UI;

import BL.Usuario;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.paint.Color;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author XchoryX
 */
public class CambiosCuenta extends javax.swing.JDialog {

    private Usuario user;
    private JLabel etiqueta;
    private boolean cambios;
    public CambiosCuenta(java.awt.Frame parent, boolean modal,Usuario t,JLabel etiq) {
        super(parent, modal);
        cambios= false;
        initComponents();
        this.txtPassword.setVisible(false);
        this.lbl.setVisible(false);
        this.btnAceptar.setVisible(false);
        user=t;
        cargar();
        this.setLocationRelativeTo(null);
        etiqueta=etiq;
    }

   private void cargar(){
       this.txtNombreUsuario.setText(user.getNombre());
       this.txtUsuario.setText(user.getUsuario());
       cargarFecha();
   }
   private void cargarFecha(){
       String var="";
       if(user.getUltFecha()!=null){
           var+=user.getUltFecha();
       }else{
           var="Primer Ingreso";
       }
       this.lblDate.setText(String.join(" ", lblDate.getText(),var));
   }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        lblDate = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtUsuario = new org.edisoncor.gui.textField.TextFieldRound();
        txtNombreUsuario = new org.edisoncor.gui.textField.TextFieldRound();
        jLabel3 = new javax.swing.JLabel();
        lblModificarNombre = new javax.swing.JLabel();
        lblModificarContraseña = new javax.swing.JLabel();
        lbl = new javax.swing.JLabel();
        txtPassword = new org.edisoncor.gui.passwordField.PasswordFieldRound();
        btnAceptar = new org.edisoncor.gui.button.ButtonRound();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(108, 131, 177));

        jLabel1.setFont(new java.awt.Font("Cambria", 0, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Información de usuario");

        lblDate.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lblDate.setForeground(new java.awt.Color(255, 255, 255));
        lblDate.setText("Ultima conexion: ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(129, 129, 129)
                .addComponent(jLabel1)
                .addContainerGap(154, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblDate)
                .addGap(91, 91, 91))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(41, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(3, 3, 3)
                .addComponent(lblDate)
                .addContainerGap())
        );

        jLabel2.setText("Usuario:");

        txtUsuario.setEditable(false);

        txtNombreUsuario.setEditable(false);

        jLabel3.setText("Nombre Completo:");

        lblModificarNombre.setText("Modificar...");
        lblModificarNombre.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblModificarNombreMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblModificarNombreMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblModificarNombreMouseExited(evt);
            }
        });

        lblModificarContraseña.setText("Modificar Contraseña");
        lblModificarContraseña.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblModificarContraseñaMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblModificarContraseñaMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblModificarContraseñaMouseExited(evt);
            }
        });

        lbl.setText("Ingresar Nueva Contraseña");

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lbl)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel3))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(34, 34, 34)
                                .addComponent(jLabel2)))
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                            .addComponent(txtNombreUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblModificarNombre)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32))))
            .addGroup(layout.createSequentialGroup()
                .addGap(196, 196, 196)
                .addComponent(lblModificarContraseña)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNombreUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(lblModificarNombre))))
                .addGap(18, 18, 18)
                .addComponent(lblModificarContraseña)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbl)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 6, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lblModificarNombreMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblModificarNombreMouseEntered
        this.lblModificarNombre.setForeground(java.awt.Color.BLUE);
    }//GEN-LAST:event_lblModificarNombreMouseEntered

    private void lblModificarNombreMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblModificarNombreMouseExited
        this.lblModificarNombre.setForeground(java.awt.Color.BLACK);
    }//GEN-LAST:event_lblModificarNombreMouseExited

    private void lblModificarContraseñaMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblModificarContraseñaMouseEntered
        this.lblModificarContraseña.setForeground(java.awt.Color.BLUE);
    }//GEN-LAST:event_lblModificarContraseñaMouseEntered

    private void lblModificarContraseñaMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblModificarContraseñaMouseExited
        this.lblModificarContraseña.setForeground(java.awt.Color.BLACK);
    }//GEN-LAST:event_lblModificarContraseñaMouseExited

    private void lblModificarContraseñaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblModificarContraseñaMouseClicked
        this.txtPassword.setVisible(true);
        this.lbl.setVisible(true);
        this.btnAceptar.setVisible(true);
    }//GEN-LAST:event_lblModificarContraseñaMouseClicked

    private void lblModificarNombreMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblModificarNombreMouseClicked
        this.btnAceptar.setVisible(true);
        this.txtNombreUsuario.setEditable(true);
    }//GEN-LAST:event_lblModificarNombreMouseClicked
    private void voidActualizar(){
        boolean te;
        try {
            te=user.realizarActualizacion(this.txtNombreUsuario.getText(),String.valueOf(this.txtPassword.getPassword()));
            if(te){
                JOptionPane.showMessageDialog(null, "El Usuario se Actualizo Correctamente", "Actualización Correcta", JOptionPane.INFORMATION_MESSAGE);
                limpiar();
         }else{
               JOptionPane.showMessageDialog(null, "Error el usuario no fue actualizado..!\nIntentelo mas tarde o comuniquese con soporte para mas información. ", "Error en la Base de Datos", JOptionPane.ERROR_MESSAGE);
            }
        cambios=te;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Error: "+ex.getMessage(), "Error en la Base de Datos", JOptionPane.ERROR_MESSAGE);
        }
    }
    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
       if(cambios){
           this.etiqueta.setText(this.txtNombreUsuario.getText());
           user.setNombre(this.txtNombreUsuario.getText());
       }
       
    }//GEN-LAST:event_formWindowClosed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        voidActualizar();
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void limpiar(){
        this.txtPassword.setVisible(false);
        this.lbl.setVisible(false);
        this.btnAceptar.setVisible(false);
        this.txtNombreUsuario.setEditable(false);
       
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.edisoncor.gui.button.ButtonRound btnAceptar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lbl;
    private javax.swing.JLabel lblDate;
    private javax.swing.JLabel lblModificarContraseña;
    private javax.swing.JLabel lblModificarNombre;
    private org.edisoncor.gui.textField.TextFieldRound txtNombreUsuario;
    private org.edisoncor.gui.passwordField.PasswordFieldRound txtPassword;
    private org.edisoncor.gui.textField.TextFieldRound txtUsuario;
    // End of variables declaration//GEN-END:variables
}
